import StoreBase, { IStoreBaseMeta } from '../../common/stores/StoreBase';
import { observable, autorun, action, computed } from 'mobx';
import {
  observablePromise,
  ObservableTempState,
  observableViewModel,
} from 'legions/store-utils';
import UiStoreBase from '../../common/stores/ui/UiStoreBase';
import { IProConditions } from 'legions-pro-design/es/LegionsProConditions/ProConditionsUtils';
import { InstanceQueryConditions } from 'legions-pro-design/es/LegionsProConditions/interface';
import { InstanceProModal } from 'legions-pro-design/es/LegionsProModal/interface';
type IQuery=Array<IProConditions['componentModel']>
import {
  ComponentViewResource,
  IResourceEvent,
  ITriggerEventPrams,
} from './even/resourceEvent';
import { fullProvider, lazyInject } from 'legions-control-container';
// tslint:disable-next-line: no-empty-interface
import { QueryTemplateConfig } from '../constants/template/query';
import { StoreModules } from 'legions/store';
import { InstanceProForm } from 'legions-pro-design/es/LegionsProForm/interface';
interface IContext { }
class ViewModel{
  /** * 活动组件类型 * @type {('text'|'select'|'textArea'|'date'|'daterange'|'checkBox'|'radioButton')}*/
  @observable componentActiveType:
    | 'text'
    | 'select'
    | 'textArea'
    | 'date'
    | 'daterange'
    | 'checkBox'
    | 'radioButton' = 'text';
}
@StoreModules
export default class ProjectQueryPropertyStore extends UiStoreBase<IContext> {
  static meta: IStoreBaseMeta = {
    ...StoreBase.meta,
    eventScopes: [ComponentViewResource],
    contextTypes: {},
  };
  
  /** 搜索条件组件实例(列表页画布条件区域组件) */
  queryComponentRef: InstanceQueryConditions = null;

  /** 搜索条件属性设置对话框 */
  queryPropertyModalRef: InstanceProModal = null;

  /** 搜索条件组件属性表单实例 */
  queryConditionsFormProperyRef: InstanceProForm = null;

  querySearchFormProperyRef: InstanceProForm = null;

  viewModel = observableViewModel<ViewModel>(new ViewModel());
  /** 正在编辑状态的组件uid */
  @observable editUid:string=''
  @lazyInject(QueryTemplateConfig)
  public queryTemplate!: QueryTemplateConfig;
  constructor(context: IContext) {
    super(context);
  }

  /***  同步正在编辑的组件uid */
  triggerSyncQueryUidEven(uid: string) {
    this.context.dispatch(ComponentViewResource.created, { uid });
  }
  @action addQuery(query: IQuery) {
    this.queryComponentRef.methods.addQuery(query);
  }
  /** 添加搜索条件项 */
  @action insertQuerys(type: string,onQueryPropertyClick:(value: { uid: string;name:string})=>void) {
    const component = type;
    switch (component) {
      case 'text':
        this.addQuery(this.queryTemplate.createText(onQueryPropertyClick));
        break;
      case 'textArea':
        this.addQuery(this.queryTemplate.createTextArea(onQueryPropertyClick));
        break;
      case 'select':
        this.addQuery(this.queryTemplate.createSelect(onQueryPropertyClick));
        break;
      case 'date':
        this.addQuery(this.queryTemplate.createDate(onQueryPropertyClick));
        break;
      case 'daterange':
        this.addQuery(this.queryTemplate.createDaterange(onQueryPropertyClick));
        break;
      case 'checkBox':
        this.addQuery(this.queryTemplate.createCheckBox(onQueryPropertyClick));
        break;
      case 'number':
        this.addQuery(this.queryTemplate.createNumberInput(onQueryPropertyClick));
        break;
      case 'radioButton':
        this.addQuery(this.queryTemplate.createRadioButton(onQueryPropertyClick));
        break;
    }
  }
  @action onEvent(event: IResourceEvent<ITriggerEventPrams>) {
    if (ComponentViewResource.created.name === event.name) {
      this.editUid = event.payload.uid;
    }
  }
}
